package com.herch.system.service.impl;

import com.herch.common.utils.CommonUtil;
import com.herch.common.utils.DateUtils;
import com.herch.common.utils.SecurityUtils;
import com.herch.system.domain.HqEquipmentInfo;
import com.herch.system.mapper.HqEquipmentInfoMapper;
import com.herch.system.service.IHqEquipmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-14
 */
@Service
public class HqEquipmentInfoServiceImpl implements IHqEquipmentInfoService {
    private final static String FZ_FRAME = " 04 00 00 00 ";
    private final static String RS485_FRAME = " 03 01 48 00 ";
    @Autowired
    private HqEquipmentInfoMapper hqEquipmentInfoMapper;

    /**
     * 查询设备信息管理
     * 
     * @param equipmentId 设备信息管理主键
     * @return 设备信息管理
     */
    @Override
    public HqEquipmentInfo selectHqEquipmentInfoByEquipmentId(String equipmentId)
    {
        return hqEquipmentInfoMapper.selectHqEquipmentInfoByEquipmentId(equipmentId);
    }

    /**
     * 查询设备信息管理列表
     * 
     * @param hqEquipmentInfo 设备信息管理
     * @return 设备信息管理
     */
    @Override
    public List<HqEquipmentInfo> selectHqEquipmentInfoList(HqEquipmentInfo hqEquipmentInfo)
    {
        return hqEquipmentInfoMapper.selectHqEquipmentInfoList(hqEquipmentInfo);
    }

    /**
     * 新增设备信息管理
     * 
     * @param hqEquipmentInfo 设备信息管理
     * @return 结果
     */
    @Override
    public int insertHqEquipmentInfo(HqEquipmentInfo hqEquipmentInfo)
    {
        hqEquipmentInfo.setCreateTime(DateUtils.getNowDate());
        hqEquipmentInfo.setCreateBy(SecurityUtils.getUsername());
        String fzs = getFrameStru(hqEquipmentInfo,FZ_FRAME,0);
        hqEquipmentInfo.setFzFrameStru(fzs);
        String rs485s = getFrameStru(hqEquipmentInfo,RS485_FRAME,1);
        hqEquipmentInfo.setRsFrameStru(rs485s);
        return hqEquipmentInfoMapper.insertHqEquipmentInfo(hqEquipmentInfo);
    }


    /**
     * 修改设备信息管理
     * 
     * @param hqEquipmentInfo 设备信息管理
     * @return 结果
     */
    @Override
    public int updateHqEquipmentInfo(HqEquipmentInfo hqEquipmentInfo)
    {
        hqEquipmentInfo.setUpdateTime(DateUtils.getNowDate());
        hqEquipmentInfo.setUpdateBy(SecurityUtils.getUsername());
        String fzs = getFrameStru(hqEquipmentInfo,FZ_FRAME,0);
        hqEquipmentInfo.setFzFrameStru(fzs);
        String rs485s = getFrameStru(hqEquipmentInfo,RS485_FRAME,1);
        hqEquipmentInfo.setRsFrameStru(rs485s);
        return hqEquipmentInfoMapper.updateHqEquipmentInfo(hqEquipmentInfo);
    }
    private String getFrameStru(HqEquipmentInfo hqEquipmentInfo,String frameType,int type) {
        String fHex = Integer.toHexString(Integer.valueOf(hqEquipmentInfo.getEquipmentAddr()));
        String nHex = null;
        if(type == 0){
            nHex = Integer.toHexString(hqEquipmentInfo.getNumOfCh());
        }else{
            nHex = Integer.toHexString(hqEquipmentInfo.getNumOfCh()*2);
        }
        if (fHex.length() < 2) {
            fHex = "0" + fHex;
        }
        if (nHex.length() < 2) {
            nHex = "0" + nHex;
        }
        String fzFrame = fHex + frameType + nHex;
        String rameStru = CommonUtil.getCRC(fzFrame);
        String fzs = fzFrame + " " + rameStru;
        return fzs;
    }
    /**
     * 批量删除设备信息管理
     * 
     * @param equipmentIds 需要删除的设备信息管理主键
     * @return 结果
     */
    @Override
    public int deleteHqEquipmentInfoByEquipmentIds(String[] equipmentIds)
    {
        return hqEquipmentInfoMapper.deleteHqEquipmentInfoByEquipmentIds(equipmentIds);
    }

    /**
     * 删除设备信息管理信息
     * 
     * @param equipmentId 设备信息管理主键
     * @return 结果
     */
    @Override
    public int deleteHqEquipmentInfoByEquipmentId(String equipmentId)
    {
        return hqEquipmentInfoMapper.deleteHqEquipmentInfoByEquipmentId(equipmentId);
    }

    /**
     * 查询设备列表
     * @return
     */
    @Override
    public List<HqEquipmentInfo> selectEquipmentName() {
        return hqEquipmentInfoMapper.selectEquipmentName();
    }
}
